/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import progin.java.GlobalRoomList;
import progin.java.Message;
import progin.java.Room;
import progin.java.User;

/**
 *
 * @author Dawn
 */
@WebServlet(name="Category", urlPatterns={"/Category"})
public class Category extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ResultSet rst=null;
        ResultSet rst2=null;
        ResultSet count=null;
        Statement stm=null;
        Statement stm2=null;

        try {
            /*
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Category</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Category at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
            Connection connection = null;
            Connection connection2 = null;

            try{
                try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                } catch (InstantiationException ex) {
                    Logger.getLogger(LoginProcess.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(LoginProcess.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            catch (ClassNotFoundException ex){
            }

            connection = DriverManager.getConnection("jdbc:mysql://localhost/chatter?user=root&password=");
            connection2 = DriverManager.getConnection("jdbc:mysql://localhost/chatter?user=root&password=");

            stm=connection.createStatement();
            stm.execute("select count(parent_id) from category where parent_id = 0");
            rst = stm.getResultSet();
            rst.next();
            int amountMain = rst.getInt(1);

            if (stm.execute("select * from category where parent_id = 0"))
            {
                rst=stm.getResultSet();
                rst.next();
                for (int i = 0; i < amountMain ; i++)
                {
                    out.println("<div id=" + rst.getString(1) + ">");
                    out.println("<p><a onclick=showRoomCategorydiaddroom('" + rst.getString(1) + "'); href='#'>" + rst.getString(3) + "</a></p>");

                    stm2=connection2.createStatement();
                    stm2.execute("select count(parent_id) from category where parent_id =" + rst.getInt(1));
                    count = stm2.getResultSet();
                    count.next();
                    int amountMain2 = count.getInt(1);
                    
                    if (amountMain2 > 0)
                    {
                        stm2.execute("select * from category where parent_id = " + rst.getInt(1));
                        rst2 = stm2.getResultSet();
                        rst2.next();
                        for (int j = 0; j < amountMain2; j++)
                        {
                            out.println("<pre><p>   <a onclick=showRoomCategorydiaddroom('" + rst2.getString(1) + "'); href='#'>" + rst2.getString(3) + "</a></p></pre>");
                            for (int k = 0; k < mRooms.size(); k++)
                            {
                                if (mRooms.get(k).getParentCategoryID() == rst2.getInt(1))
                                {
                                    out.println("<pre>      <p><div class=showroom id=room" + mRooms.get(k).getParentCategoryID() + "><a onclick=chatWithRoom(opener.document.getElementById('username').value,'" + mRooms.get(k).getRoomName() + "'); href='#'>" + mRooms.get(k).getRoomName() + "</a></div></p></pre>");
                                }
                            }
                            rst2.next();
                        }
                    }

                    for (int k = 0; k < mRooms.size(); k++)
                    {
                        if (mRooms.get(k).getParentCategoryID() == rst.getInt(1))
                        {
                            out.println("<pre><p><div class=showroom id=room" + mRooms.get(k).getParentCategoryID() + "><a onclick=chatWithRoom(opener.document.getElementById('username').value,'" + mRooms.get(k).getRoomName() + "'); href='#'>" + mRooms.get(k).getRoomName() + "</a></div></p></pre>");
                        }
                    }

                    out.println("</div>");
                    rst.next();
                }
            }

        } finally {
            if (rst != null)
            {
                rst.close();
            }
            if (rst2 != null)
            {
                rst2.close();
            }
            if (stm != null)
            {
                stm.close();
            }
            if (stm2 != null)
            {
                stm2.close();
            }
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    /*****************************Procedur dan function yang dibutuhkan*************************/
    CopyOnWriteArrayList<Room> mRooms = GlobalRoomList.globalRooms;

}
